package dd.prog2.musterExercises.ws12set03_function;

import java.awt.Color;

import javax.swing.JOptionPane;

import rl.util.painttool.AbstractController;
import rl.util.painttool.PaintTool;

public class FunctionPlotterDemo extends AbstractController {

	private boolean coordinateSystemAdded;
	private Function f;
	private int count = 0;
	private double delta = 0.01;

	@Override
	public String getTitle() {
		return "Function Plotter Demo";
	}

	@Override
	public String[] getButtonNames() {
		return new String[] { "f(x) = x^2 - 2x + 2", "g(x) = 1 / x",
				"h(x) = 1 / (x^2 - 2x + 2)", "Derive", "delta=0.001",
				"delta=0.01", "delta=0.1", "?" };
	}

	@Override
	public void onButtonPressed(PaintTool ptool, int button) {
		FunctionPlotter plotter = (FunctionPlotter) ptool;
		Function fNew = null;
		if (!coordinateSystemAdded && button != -1) {
			plotter.setColor(Color.BLACK);
			plotter.addCoordinateSystem(-10, 10, -10, 10);
			coordinateSystemAdded = true;
		}
		switch (button) {
		case -1:
			coordinateSystemAdded = false;
			f = null;
			count = 0;
			break;
		case 0:
			fNew = new Parabel(1, -2, 2);
			break;
		case 1:
			fNew = new Hyperbel(1);
			break;
		case 2:
			fNew = new ComposedFunction(new Parabel(1, -2, 2), new Hyperbel(1));
			break;
		case 3:
			if (f != null) {
				fNew = new Derivation(f, delta);
			}
			break;
		case 4:
			delta = 0.001;
			break;
		case 5:
			delta = 0.01;
			break;
		case 6:
			delta = 0.1;
			break;
		case 7:
			JOptionPane
					.showMessageDialog(ptool.getCanvas(),
							"Function Plotter Demo, Version 1.0"
									+ "\nHave fun, R. Lunde",
							"About Function Plotter Demo",
							JOptionPane.INFORMATION_MESSAGE);
			break;
		}	
		if (fNew != null) {
			plotter.setColor(new Color(Math.max(255 - 20 * count, 100), Math
					.min(20 * count, 100), Math.min(20 * count, 100)));
			plotter.addFunction(fNew);
			count++;
			f = fNew;
		}
	}

	public static void main(String[] args) {
		FunctionPlotter plotter = new FunctionPlotter(new FunctionPlotterDemo());
		plotter.setVisible(true);
		// plotter.addCoordinateSystem(-2, 2, -2, 2);
		// plotter.addFunction(new Parabel(0, 1, 0));
	}

}
